package com.apes.fico.finance.respository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Created by lishikun on 2021/1/29 16:23
 */
@Component
public class StoreTasksDao extends ApesDao {

    /**
     * 门店经营任务
     * @param param
     * @return
     */
    public JSONArray queryAllTransactionFlow(JSONObject param) {
        JSONObject request = new JSONObject();
        String sql = "SELECT a.dept_id, to_char(a.year_month, 'yyyy-mm') year_month,\n" +
                "       a.business_amt, a.business_tasks_amt, a.gross_profit_tasks_amt,\n" +
                "       a.gross_profit_amt, CASE WHEN  a.business_tasks_amt  > 0 THEN ROUND(A.business_amt  / a.business_tasks_amt,2) ELSE 0 END subsidy_Amt,\n" +
                "        CASE WHEN  a.gross_profit_tasks_amt  > 0 THEN ROUND(a.gross_profit_amt  / a.gross_profit_tasks_amt ,2) ELSE 0 END reward_Punishment_Amt, a.summary\n" +
                "  FROM gl_store_tasks a\n" +
                " where 1=1";
        if (Objects.nonNull(param.get("yearMonth"))) {
            String yearMonth = param.getString("yearMonth");
            sql += " AND to_char(a.year_month,'yyyy-mm') = substr('"+ yearMonth +"',0,7)";
        }

        if (Objects.nonNull(param.get("dept"))) {
            JSONObject dept = param.getJSONObject("dept");
            request.put("dept", dept.getString("id"));
            sql += " and a.dept_id = :dept ";
        }
        return toJSONArray(find(sql, request));
    }

}
